<?php
class CommentsController extends AppController {

    public $name = 'Comments';
    public $components = array('RequestHandler');
    protected $searchable = array(
        'client' => null,
        'content' => null,
        'created' => null,
        'post_id' => null,
        'user_id' => null
    );
    public $paginate = array(
        'order' => 'Comment.created DESC',
        'contain' => array(
            'Post' => array('id', 'title', 'comment_count'),
            'User' => array('fields'=>array('id', 'username', 'nickname', 'email'))
        )
    );

    public function index(){
        $this->_justifyPaginator();
        $this->set('comments', $this->paginate());
    }

    /* Ajax leave a comment: we have to check user_id and post_id */
    public function add(){
        if(!empty($this->data)){
            /* check for post_id and user_id*/
            if(empty($this->data['Comment']['post_id']) || empty($this->data['Comment']['user_id'])){
                $this->Response->json(false, __('Invalid Comment add request, Post or User not specified', true));
            }else{
                /* Get client ip and try to save comment data */
                $this->Comment->create();
                $this->data['Comment']['client'] = $this->RequestHandler->getClientIP();
                $result = $this->Comment->save($this->data);
                if($result){
                    App::import('Helper', 'Time');
                    $time = new TimeHelper();
                    $comment = $this->Comment->getLastComment();
                    $comment['Comment']['created'] = $time->format($this->options['longDateFormat'], $comment['Comment']['created']);
                    $this->Response->json(true, __('The Comment has been saved', true), $comment);
                }else{
                    $this->Response->json(false, __('The Comment could not be saved. Please, try again.', true), $this->data);
                }
            }
        }else{
            $this->Response->json(false, __('Invalid Comment add request, no data provided', true));
        }
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid Comment', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->Comment->save($this->data)){
                $this->Session->setFlash(__('The Comment has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Comment could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->Comment->read(null, $id);
        }
        $posts = $this->Comment->Post->find('list');
        $users = $this->Comment->User->find('list');
        $this->set(compact('posts','users'));
    }

}
?>